package com.itheima.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.entity.Employee;
import com.itheima.result.R;
import com.itheima.service.EmployeeService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.DigestUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.time.LocalDateTime;

@RestController
@RequestMapping("/employee")
@Slf4j
public class EmployeeController {
    @Autowired
    private EmployeeService employeeService;

    /**
     * 登录功能
     *
     * @param request  SEEsion
     * @param employee 接受参数
     * @return
     */
    @PostMapping("/login")
    R<Employee> login(HttpServletRequest request, @RequestBody Employee employee) {
        String password = employee.getPassword();
        password = DigestUtils.md5DigestAsHex(password.getBytes());
        LambdaQueryWrapper<Employee> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(employee.getUsername() != null, Employee::getUsername, employee.getUsername());
        Employee one = employeeService.getOne(queryWrapper);
        if (one == null) {
            return R.error("用户名不存在");
        }
        if (!one.getPassword().equals(password)) {
            return R.error("密码错误");
        }
        if (one.getStatus() == 0) {
            return R.error("用户已封禁");
        }
        request.getSession().setAttribute("employee", one.getId());
        System.out.println(one.getId() + "      " + employee.getId());
        return R.success(one);

    }

    @PostMapping("/logout")
    R<Employee> logut(HttpServletRequest request) {
        request.removeAttribute("employee");
        return R.success(null);
    }

    @PostMapping
    public R<Employee> addUser(HttpServletRequest request, @RequestBody Employee employee) {
        String password = "123456";
        password = DigestUtils.md5DigestAsHex(password.getBytes());
        employee.setCreateTime(LocalDateTime.now());
        employee.setUpdateTime(LocalDateTime.now());
        employee.setStatus(1);
        employee.setCreateUser((Long) request.getSession().getAttribute("employee"));
        employee.setUpdateUser((Long) request.getSession().getAttribute("employee"));
        employee.setPassword(password);
        boolean save = employeeService.save(employee);
        return R.success(null);
    }

    @GetMapping("/page")
    public R<Page> pageSerach(int page, int pageSize, String name) {
        log.info("开始放行了:page={},pageSiz={},name={}", page, pageSize, name);
        Page pageInfo = new Page(page, pageSize);
        LambdaQueryWrapper<Employee> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.like(StringUtils.isNotBlank(name), Employee::getName, name);
        queryWrapper.orderByDesc(Employee::getUpdateTime);
        employeeService.page(pageInfo, queryWrapper);
        return R.success(pageInfo);
    }

    @PutMapping
    public R<String> privilegeModify(HttpServletRequest request, @RequestBody Employee employee) {
        LambdaQueryWrapper<Employee> queryWrapper = new LambdaQueryWrapper<>();
        Long empId = (Long) request.getSession().getAttribute("employee");
        queryWrapper.eq(Employee::getId, employee.getId());
        Boolean b = employeeService.updateById(employee);
        return b ? R.success("提交成功") : R.error("提交失败");
    }

    @GetMapping("/{id}")
    public R<Employee> serachUser(@PathVariable Long id) {
        log.info("id是:{}", id);
        Employee byId = employeeService.getById(id);
        if (byId != null) {
            return R.success(byId);
        }
        return R.error("ID传入错误");
    }


}
